package com.wanmait.gohome.filter;

import com.wanmait.gohome.pojo.Admin;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
//过滤器没登陆不能访问后台界面
@WebFilter("/manage/*")
public class AdminLoginFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest)req;
        HttpServletResponse response = (HttpServletResponse) resp;
        String path = request.getRequestURI();
        String queryString = request.getQueryString();

        //访问路径
        if(path.contains("/manage/login")||path.contains("/manage/checkCode")){
            chain.doFilter(req, resp);
            return;
        }
        HttpSession session = request.getSession();
        Admin admin = (Admin)session.getAttribute("admin");
        if(admin==null){
            response.sendRedirect(request.getContextPath()+"/manage/login");
        }
        else {
            chain.doFilter(req, resp);
        }
    }
    public void init(FilterConfig config) throws ServletException {

    }

}
